package com.github.wj.web.service.impl;

import com.github.wj.orm.security.datasource.DBUtil;
import com.github.wj.web.domain.DmjzxDomain;
import com.github.wj.web.entity.TSjzxDmjzx;
import com.github.wj.web.service.CustomDmjzxService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Create by IntelliJ IDEA
 * 用户：王建
 * 日期：2018/3/18
 */
@Service
public class CustomDmjzxServiceImpl implements CustomDmjzxService {

    @Autowired
    private DBUtil dbUtil;

    @Autowired
    private DmjzxDomain dmjzxDomain;

    @Cacheable(value = "dictionary",key = "#ssdm")
    @Override
    public List<TSjzxDmjzx> getAllBySsdm(String ssdm) {
        return dbUtil.getAllBeanList("select id,name from t_sjzx_dmjzx t where t.ssdm = ? and t.zt =1 ",TSjzxDmjzx.class,ssdm);
    }

    @Cacheable(value = "dictionaryParent",key = "#ssdm")
    @Override
    public List<TSjzxDmjzx> getAllBySsdmAndGrade(String ssdm, Long grade) {
        return dbUtil.getAllBeanList("select id,name from t_sjzx_dmjzx t where t.ssdm = ? and t.zt =1 and t.grade = ?",TSjzxDmjzx.class,ssdm,grade);
    }

    @Override
    public List<TSjzxDmjzx> getAllBySSdmAndSsdm(String ssdm, String dm) {
        String sql="select id,name from t_sjzx_dmjzx t where t.ssdm = ? and t.zt =1 and t.dm like ? ";
        return dbUtil.getAllBeanList(sql,TSjzxDmjzx.class,ssdm,dm);
    }

    @Override
    public List<TSjzxDmjzx> getAllById(Long id,int num) {
        String info="00";
        if(num==4){
            info="";
        }
        TSjzxDmjzx entity=dmjzxDomain.findOne(id);
        if(null!=entity){
            String sql="select id,name from t_sjzx_dmjzx t where t.ssdm = ? and t.zt =1 and t.dm like ? and id !=?";
            return dbUtil.getAllBeanList(sql,TSjzxDmjzx.class,entity.getSsdm(),entity.getDm().substring(0,num)+"%"+info,id);
        }
       return null;
    }
}
